package com.myblog.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myblog.constant.RedisHashKey;
import com.myblog.constant.RedisKey;
import com.myblog.mapper.*;
import com.myblog.model.ArticleLikes;
import com.myblog.model.CommentLikes;
import com.myblog.model.LeaveLikes;
import com.myblog.model.User;
import com.myblog.service.LikeService;
import com.myblog.service.impl.redis.HashRedisServiceImpl;
import com.myblog.utils.DataMap;
import com.myblog.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LeaveMapper leaveMapper;

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 获取文章点赞列表，分页
     * @param rows
     * @param pageNum
     * @return
     */
    @Override
    public DataMap getArticleLikesByPage(Integer rows, Integer pageNum) {
        PageHelper.startPage(pageNum,rows);
//        List<Integer> integers = likeMapper.selectAllArticleLikesId();
        List<ArticleLikes> articleLikes = likeMapper.selectAllArticleLikes();
        PageInfo<ArticleLikes> pageInfo = new PageInfo<>(articleLikes);

        JSONObject result = new JSONObject();

        Integer NumOfMsgNotRead = likeMapper.selectMsgIsNotReadNum();

        //未读消息
        result.put("msgIsNotReadNum",NumOfMsgNotRead);

        JSONArray jsonArray = new JSONArray();

        //将文章点赞信息封装
        for (ArticleLikes articleLike : articleLikes) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",articleLike.getId());
            jsonObject.put("isRead",articleLike.getIsRead());
            //点赞的人的id
            Integer likerId = articleLike.getLikerId();
            String likerName = userMapper.selectUserNameById(likerId);
            jsonObject.put("praisePeople",likerName);
            jsonObject.put("articleId",articleLike.getArticleId());
            //根据文章id搜索搜索文章，获取文章的信息
            String articleTitle = articleMapper.selectArticleTitleByArticleId(articleLike.getArticleId());
            jsonObject.put("articleTitle",articleTitle);
            jsonObject.put("likeDate",articleLike.getLikeDate());
            jsonArray.add(jsonObject);
        }

        result.put("result",jsonArray);

        //将分页封装
        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum",pageInfo.getPageNum());
        pageJson.put("pageSize",pageInfo.getSize());
        pageJson.put("total",pageInfo.getTotal());
        pageJson.put("pages",pageInfo.getPages());
        pageJson.put("isFirstPage",pageInfo.isIsFirstPage());
        pageJson.put("isLastPage",pageInfo.isIsLastPage());

        result.put("pageInfo",pageJson);

        return DataMap.success().setData(result);
    }

    @Override
    public DataMap setAllLikesIsRead() {
        likeMapper.updateAllArticleLikesIsRead();
        return DataMap.success();
    }

    /**
     * 根据用户和文章id判断用户是否已经点赞了文章
     * @param articleId
     * @param userName
     * @return
     */
    @Override
    public boolean isLike(long articleId, String userName) {
        User user = userMapper.selectUserByName(userName);
        ArticleLikes articleLikes = likeMapper.isLike(articleId,user.getId());
        return articleLikes!=null;
    }

    /**
     * 通过文章id更新文章的点赞数量+1
     * @param articleId
     * @return
     */
    @Override
    public DataMap updateLikeByArticleId(long articleId) {
        //点赞数+1
        articleMapper.updateLikeByArticleId(articleId);
        //获取点赞后的点赞数
        int likes = articleMapper.queryLikesByArticleId(articleId);
        return DataMap.success().setData(likes);
    }

    /**
     * 将点赞信息传入文章_点赞_关系表
     * @param likeOfArticle
     */
    @Override
    public void insertArticleLikesRecords(ArticleLikes likeOfArticle) {
        likeMapper.insertArticleLikesRecords(likeOfArticle);
    }

    @Override
    public boolean leaveIsLike(String pageName,Integer leaveId, String userName) {
        User user = userMapper.selectUserByName(userName);
        LeaveLikes leaveLikes;
        leaveLikes = likeMapper.LeaveIsLike(pageName,leaveId,user.getId());
        return leaveLikes!=null;
    }

    @Override
    public DataMap updateLeaveLike(Integer leaveId) {
        //点赞数+1
        leaveMapper.updateLikeById(leaveId);
        //获取点赞后的点赞数
        int likes = leaveMapper.queryLikesById(leaveId);
        return DataMap.success().setData(likes);
    }

    @Override
    public void insertLeaveLikesRecords(LeaveLikes likeOfLeave) {
        likeMapper.insertLeaveLikesRecords(likeOfLeave);
    }

    @Override
    public boolean commentIsLike(long articleId, Integer commentId, String userName) {
        User user = userMapper.selectUserByName(userName);
        CommentLikes commentLikes = likeMapper.commentIsLike(articleId,commentId,user.getId());
        return commentLikes!=null;
    }

    @Override
    public DataMap updateCommentLike(Integer commentId) {
        //点赞数+1
        commentMapper.updateLikeById(commentId);
        //获取点赞后的点赞数
        int likes = commentMapper.queryLikesById(commentId);
        return DataMap.success().setData(likes);
    }

    @Override
    public void insertCommentLikesRecords(CommentLikes likeOfComment) {
        likeMapper.insertCommentLikesRecords(likeOfComment);
    }
}
